<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_product_current_content($function) {
    global $e107shop_db_name, $sql, $ns;
    
    if (!$sql->db_Select($e107shop_db_name['custom'], "*", "r_type=2")) {
        $ns->tablerender("<center>".lan_admin_menu_custom_content."</center>", 
        "<br /><center>".lan_admin_prod_no_content_error."<center><br />");
        return;
    } else {
        if ($function) {
            $display="<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_content_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_content_attributes.": <select class=\"tbox\" name='p' id='p'>";
                                
            while($row=$sql->db_Fetch()){
                    $display.="<option value='".$row["id"]."'>".$row["r_name"]."</option>";
            }
                
            $display.="</option></select>&nbsp;&nbsp;";

            $display.="<input class=\"tbox\" type='button' name='a' value='"
            .lan_global_edit."' OnClick=\"window.location='admin_custom_content.php?a=1&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
            $display.="<input class=\"tbox\" type='button' name='d' value='"
            .lan_global_delete."' OnClick=\"window.location='admin_custom_content.php?a=2&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="</form>";
            $display.="</td></tr></table>";
        } else {
            $display="<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_content_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_content_attributes.": <select class=\"tbox\" name='p' id='p'>";
            while($row=$sql->db_Fetch()) {
                $display.="<option value='".$row["id"]."'>".$row["r_name"]."</option>";
            }

            $display.="</select><input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' OnClick=\"window.location='admin_custom_content.php?a=1&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' OnClick=\"window.location='admin_custom_content.php?a=2&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="</form>";
            $display.="</td></tr></table>";
        }
    }
    $ns->tablerender("<center>".lan_admin_menu_custom_content."</center>", $display);
}

function e107shop_manage_content($action, $attribute) {
    global $e107shop_db_name, $sql, $ns, $tp;
    if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['custom'], "*", "id=".$attribute);
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $function_name = $row["r_name"];
            $function_content=$tp->toHTML($row["content"]);
            $content_title = $row["title"];
            $position = $row["position"];
                        
        }
        $formaction = 1;
    } else if($action==2) {
        $sql->db_Delete($e107shop_db_name['custom'], "ID=".$attribute);
        
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<br /><center>".lan_global_deleted."</center><br />");
        $formaction = 2;
    } else {
        $formaction = 2;
    }

    //
    // Display form
    //

    $display = "<center><form action='".e_SELF."' method='POST' name='productform' ID='productform'><br />
                <table width='95%' class='fborder'>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_content_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='function_title' value='".$content_title."'>
                        </td>
                    </tr>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_content_page.": 
                        </td>
                        <td class='forumheader3'>
                        ".e107shop_get_page_name_drop($function_name)."
                        </td>
                    </tr>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_content_position.": 
                        </td>
                        <td class='forumheader3'>
                            <select name='position' class='tbox'>";
                        if($position == "top"){
                            $display .= "<option selected value='top' value='top'>Top</option>
                                    <option value='bottom' value='bottom'>Bottom</option>";
                        } else {
                            $display .= "<option value='top' value='top'>Top</option>
                                    <option selected value='bottom' value='bottom'>Bottom</option>";
                        }
                        
                    $display.= "</select>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_content_content.": 
                        </td>
                        <td class='forumheader3'>
                            <textarea cols=50 rows=10 class=\"tbox\" name='function'>".$function_content."</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='".$attribute."' name='attributeid'><center>";
    if($action==1) {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'>";
    } else {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'>";
    }
    
    $display .= "</center></td></tr></form></table>";
    
    if ($action == 1) {
        $ns->tablerender("<center>".lan_admin_content_edit, $display);
    } else {
        $ns->tablerender("<center>".lan_admin_content_add, $display);
    }
}


function e107shop_process_content($action, $function_name, $function_value, 
$attribute, $position, $function_title) {
    global $e107shop_db_name, $sql, $ns, $tp;
    
    if($action==1) {
        //
        // edit
        //
        if(!$sql->db_Update($e107shop_db_name['custom'], "title='".$function_title."', r_name='"
        .$function_name."', content='".$tp->toDB($function_value)."', position='".$position."' where ID="
        .$attribute)) {
            $ns->tablerender("<center>".lan_global_status."</center>", 
            "<br /><center>".lan_admin_content_error. ": ".mysql_error()."</center><br />");
        } else {
            $ns->tablerender("<center>".lan_global_status."</center>", 
            "<br /><center>".lan_global_updated."</center><br />");
        }
    } else {
        //
        // Insert a new product
        //
        if(!$sql->db_Insert($e107shop_db_name['custom'], "0, '".$function_title."', '".$function_name."', '2', '"
        .$position."', '".$tp->toDB($function_value)."'")) {
            $ns->tablerender("<center>".lan_global_status."</center>", 
            "<br /><center>".lan_admin_content_error. ": ".mysql_error()."</center><br />");
        } else {
            $ns->tablerender("<center>".lan_global_status."</center>", 
            "<br /><center>".lan_admin_content_inserted."</center><br />");
        }
    }
}

function e107shop_get_page_name_drop($function_name) {
    $pages = "cart,index,products,payment,product_details,order,checkout,download";
    $pages = explode(",", $pages);
    $select = "<select class='tbox' name='function_name'>";
    foreach($pages as $page) {
        if($function_name==$page) {
            $select .= "<option selected value='".$function_name."'>".$function_name."</option>";
        } else {
            $select .= "<option value='".$page."'>".$page."</option>";
        }
    }
    $select .= "</select>";
    return $select;
}
